<template>
  <el-card class="container">
    <el-form inline :disabled="disabled">
      <el-form-item label="一级分类" class="category-selector-item">
        <!--
          1. v-model绑定计算属性
            {
              get() { return xxx },
              set() { 发送请求 }
            }
          2. value属性
             change事件  事件参数就是选中的分类id
         -->
        <el-select
          :value="category1Id"
          placeholder="请选择一级分类"
          @change="getCategory2List"
        >
          <!--
            label 选项显示的内容
            value 选项选中时得到的值
           -->
          <el-option
            v-for="c1 in category1List"
            :key="c1.id"
            :label="c1.name"
            :value="c1.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="二级分类" class="category-selector-item">
        <el-select
          :value="category2Id"
          placeholder="请选择二级分类"
          @change="getCategory3List"
        >
          <!--
            label 选项显示的内容
            value 选项选中时得到的值
           -->
          <el-option
            v-for="c2 in category2List"
            :key="c2.id"
            :label="c2.name"
            :value="c2.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="三级分类" class="category-selector-item">
        <el-select
          :value="category3Id"
          placeholder="请选择三级分类"
          @change="SET_CATEGORY3ID"
        >
          <!--
            label 选项显示的内容
            value 选项选中时得到的值
           -->
          <el-option
            v-for="c3 in category3List"
            :key="c3.id"
            :label="c3.name"
            :value="c3.id"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'

export default {
  name: 'CategorySelector',
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      value1: '',
      value2: '',
      value3: ''
    }
  },
  computed: {
    ...mapState('category', [
      'category1List',
      'category2List',
      'category3List',
      'category1Id',
      'category2Id',
      'category3Id'
    ])
  },
  mounted() {
    this.getCategory1List()
  },
  beforeDestroy() {
    this.RESET()
  },
  methods: {
    ...mapActions('category', [
      'getCategory1List',
      'getCategory2List',
      'getCategory3List'
    ]),
    ...mapMutations('category', ['SET_CATEGORY3ID', 'RESET'])
  }
}
</script>

<style lang="sass" scoped>
.category-selector-item
  margin-top: 10px
  margin-bottom: 10px
</style>
